/***************************************************************************
 创建者: hualei
 开始时间: 2018-10-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  WAIT_COMMAND 等待命令指令具体实现
 ***************************************************************************/
#include "regdata.h"
#include "GeneralDefine.h"
#include "programengine.h"
#include "waitcommandinstructionlogic.h"
#include "messagelog.h"

WaitCommandInstructionLogic::WaitCommandInstructionLogic(int robotIdIn,ProgramSet* programSetIn,
                                                         RegData* regDataIn,ProgramEngine* programEngineIn)
{
    programSet=programSetIn;
    robotId=robotIdIn;
    programEngine=programEngineIn;

    regData=regDataIn;
}
E_PROGRAM_LOOP_RESULT WaitCommandInstructionLogic::startWaitCommand(int programNumIn, const WaitCommandInstruction &instructionIn, int lineIndex)
{
    E_PROGRAM_LOOP_RESULT endFlag = E_PROGRAM_LOOP_RESULT_NORMAL;

    int queueSize = 0;
    //等待时间
    bool queueEmpty = true;
    QTime time;
    time.start();
    int timeElapsed = time.elapsed();
    while(timeElapsed < instructionIn.waitMaxTime*1000)
    {
        protectMutex.lock();
        queueSize = commandQueue.size();
        protectMutex.unlock();
        if(queueSize>0)
        {
            queueEmpty=false;
            break;
        }
        usleep(1000);
        timeElapsed = time.elapsed();
        if(ENUM_COMMAND_CEASE == programEngine->getEngineStatue() ||
                ENUM_COMMAND_RESET == programEngine->getEngineStatue())
        {
            qDebug()<<"programEngineSatus=="<<programEngine->getEngineStatue();
           return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
        }
    }

    if(queueEmpty)
    {
        return programEngine->startJump(programNumIn,instructionIn.timeOutLabel);
    }
    else
    {
        //取出数据操作后开始执行下一条
        (*programSet)[programNumIn].timeOutFlag = false;

        UserCommandData suUserCommandData;
        protectMutex.lock();
        suUserCommandData = commandQueue.front();
        commandQueue.pop_front();
        protectMutex.unlock();
        regData->setRRegValue(instructionIn.registerIndex-1,suUserCommandData.targetPosition);
        programEngine->calculateNextLineToRun(programNumIn);
    }

    return endFlag;
}

int WaitCommandInstructionLogic::reset()
{
    protectMutex.lock();
    commandQueue.clear();
    protectMutex.unlock();
    return 1;
}

int WaitCommandInstructionLogic::addCommand(UserCommandData commandIn)
{
    protectMutex.lock();
    //只换成一个命令。
    commandQueue.clear();
    commandQueue.enqueue(commandIn);
    protectMutex.unlock();
    return 1;
}
void WaitCommandInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {

    case 0:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr("; %1,第%2行， IO号超出已有逻辑ＩＯ范围，请检查指令或ＩＯ配置，").arg(message).arg(parameter3)
                +QString::number(parameter1) + ">="
        +QString::number(parameter2);
        break;


    }



    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
